package com.yztc.youbadbad.ui.homepage;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.yztc.youbadbad.App;
import com.yztc.youbadbad.R;
import com.yztc.youbadbad.adapter.ContentRecyclerAdapter;
import com.yztc.youbadbad.bean.homepage.ContentBean;
import com.yztc.youbadbad.bean.homepage.DeserveBean;
import com.yztc.youbadbad.presenter.DeservePresenterImpl;
import com.yztc.youbadbad.ui.homepage.interfaces.IDeserveView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HaowuHomeFragment extends Fragment implements IDeserveView {
    @BindView(R.id.haowu_rv)
    RecyclerView haowuRv;
    @BindView(R.id.swipe_haowu)
    SwipeRefreshLayout swipeHaowu;
    @BindView(R.id.progressBar_haowu)
    ProgressBar progressBarHaowu;

    private Context mContext = App.app;
    private int mCurrentPage = 1;
    private int mLastVisibleItem = 0;

    private List<ContentBean> mContentList = new ArrayList<>();
    private ContentRecyclerAdapter mAdapter;
    private LinearLayoutManager mManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_haowu_home, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        loadInterworkData();
        initView();
    }

    private void loadInterworkData() {

        DeservePresenterImpl presenter = new DeservePresenterImpl(this);
        presenter.getDeserveData(mCurrentPage);
    }


    private void initView() {
        mManager = new LinearLayoutManager(mContext);
        mAdapter = new ContentRecyclerAdapter(mContext, mContentList);
        haowuRv.setAdapter(mAdapter);
        haowuRv.setLayoutManager(mManager);

        //下来刷新监听
        swipeHaowu.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                loadInterworkData();
            }
        });

        //设置RecyclerView的滚动监听，目的是实现上拉加载下一页
        haowuRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView,
                                             int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && mLastVisibleItem == mAdapter.getItemCount() - 1) {
                    mCurrentPage++;
                    loadInterworkData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                mLastVisibleItem = mManager.findLastVisibleItemPosition();
            }
        });
    }


    @Override
    public void getDeserveData(DeserveBean deserveBean) {
        List<ContentBean> list = deserveBean.getData().getList();
        if (mCurrentPage == 1) {
            mAdapter.reloadRecyclerView(list, true);
        } else {
            mAdapter.reloadRecyclerView(list, false);
        }
        //结束刷新
        swipeHaowu.setRefreshing(false);
    }

    @Override
    public void showProgressBar() {
        progressBarHaowu.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgressBar() {
        progressBarHaowu.setVisibility(View.GONE);
    }

    @Override
    public void showErrorMessage() {
        Toast.makeText(mContext, "error", Toast.LENGTH_SHORT).show();
        //结束刷新
        swipeHaowu.setRefreshing(false);
    }
}
